/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.siseventos.dao.da;

import br.siseventos.dao.util.HibernateUtil;
import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Hiragi
 */
public class BaseDAO<Tab> {

    private Class<Tab> classe;
    protected Session hib;

    public BaseDAO() {
        classe = (Class<Tab>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        hib = HibernateUtil.getSessionFactory().openSession();
    }

    public Class<Tab> getClasse() {
        return this.classe;
    }

    public Tab incluir(Tab obj) {
        Transaction ts = hib.beginTransaction();
        ts.begin();
        hib.persist(obj);
        hib.flush();
        ts.commit();
        return obj;
    }

    public Tab alterar(Tab obj) {
        Transaction ts = hib.beginTransaction();
        ts.begin();
        hib.saveOrUpdate(obj);
        hib.flush();
        ts.commit();
        return obj;
    }

    public Tab excluir(int idt) {
        Transaction ts = hib.beginTransaction();
        ts.begin();
        Tab obj = consultarPorIdt(idt);
        hib.delete(obj);
        hib.flush();
        ts.commit();
        return obj;
    }

    public Tab consultarPorIdt(int idt) {
        Tab obj = null;
        Query qy = hib.createQuery("SELECT obj FROM "
                + getClasse().getSimpleName() + " obj WHERE idt" + getClasse().getSimpleName().substring(2) + "=?");
        qy.setInteger(0, idt);
        obj = (Tab) qy.uniqueResult();
        return obj;
    }

    public List<Tab> consultarTodos() {
        List<Tab> lista = null;
        Query qy = hib.createQuery("SELECT obj FROM " + getClasse().getSimpleName() + " obj ");
        lista = qy.list();
        return lista;
    }

    public List<Tab> consultarTodos(int indiceComeco, int count) {
        List<Tab> lista = null;
        Query qy = hib.createQuery("SELECT obj FROM " + getClasse().getSimpleName() + " obj ");
        qy.setFirstResult(indiceComeco);
        qy.setMaxResults(count);
        lista = qy.list();
        return lista;
    }

    public List<Tab> consultarPorNme(String nme) {
        List<Tab> lista = null;
        Query qy = hib.createQuery("SELECT obj FROM "
                + getClasse().getSimpleName() + " obj WHERE nme"
                + getClasse().getSimpleName().substring(2) + " LIKE ?");
        qy.setString(0, "%" + nme + "%");
        lista = qy.list();
        return lista;
    }

    public Tab consultarPorNmeExato(String nome) {
        Tab r;
        Query qy = hib.createQuery("SELECT obj FROM "
                + getClasse().getSimpleName() + " obj WHERE nme"
                + getClasse().getSimpleName().substring(2) + " = :nome");
        qy.setParameter("nome", nome, Hibernate.STRING);
        r = (Tab) qy.uniqueResult();
        return r;
    }
    
    // Método utilitário para usar com a geração de Relatório
    public Connection getConexao(){
        return hib.connection();
    }
}
